package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.CreditAssignService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;

@Controller
@RequestMapping("/credit")

public class CreditAssignAction  implements Serializable{
	private static final Log logger = LogFactory.getLog(CreditAssignAction.class);

	@Autowired
	private CreditAssignService creditAssignService;

	
	/**
	 * E330015 债权转让
	 * 
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	/*@RequestMapping(value = "/assign")
	public void dealCreditAssign(HttpServletRequest request, HttpServletResponse response){
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String creditAcctCIDs = paramsMap.get("creditAcctCIDs");   //债权子户ID字符串
		String signFlag = paramsMap.get("signFlag");   //签约中金额退出标志：0,不退出签约中金额;1,退出签约中金额
		String userId = paramsMap.get("userId");   //用户id
		String orderId = paramsMap.get("orderId");   //计划编号(订单id)
		
		logger.debug("接收到的数据是:债权子户ID:" + creditAcctCIDs + " || 签约中金额退出标志:" + signFlag);
		if (StringUtils.isBlank(userId) || StringUtils.isBlank(signFlag)) {
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), 
				Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/credit/assign 传入参数为空-----userId或者signFlag");
			return;
		}
		
		// 退出签约中金额
		if (!("0".equals(signFlag) || "1".equals(signFlag))) {
			// 非法参数
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), 
				Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/credit/assign 非法参数--签约中金额退出标志");
			return;
		}
		
		if("1".equals(signFlag) && StringUtils.isBlank(orderId)){
			// 订单id
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), 
					Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/credit/assign 传入参数为空----orderId");
			return;
		}
		
		try{
			String transCode = "E330015";
			// 设置报文体报文数据
			Map<String, Object> pageModelForCreditAssign = new HashMap<String, Object>();

			pageModelForCreditAssign.put("creditAcctCIDs", creditAcctCIDs); // 用户id
			pageModelForCreditAssign.put("signFlag", signFlag);
			pageModelForCreditAssign.put("userId", userId);
			pageModelForCreditAssign.put("orderId", orderId);
			
			// 构建json字符串
			String jsonStr = CommonUtil.constructJsonStr(transCode, userId,
				pageModelForCreditAssign);

			// 调用核心接口E330015债权转让
			String result = creditAssignService.dealCreditAssign(jsonStr);

			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		}catch(Exception e){
			logger.debug("/credit/assign-----" + e.getMessage());
			
			String result = CommonUtil.setResultStringCn(
				new HashMap<String,Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		}
		return;
	}*/
	
	/**
	 *  一键转让(E330017,E330015)
	 * 
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/quickCreditAssign")
	public void quickCreditAssign(HttpServletRequest request, HttpServletResponse response){
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
//		String signFlag = paramsMap.get("signFlag");   //签约中金额退出标志：0,不退出签约中金额;1,退出签约中金额
		String userId = paramsMap.get("userId");   //用户id
		String orderId = paramsMap.get("orderId");   //计划编号(订单id)
		
		logger.debug("接收到的数据是:订单id:" + orderId + "用户id:" + userId);
		if (StringUtils.isBlank(userId) || StringUtils.isBlank(orderId)) {
			logger.debug("/credit/quickCreditAssign 传入参数为空");
			String result = CommonUtil.setResultStringCn(
				new HashMap<String,Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
		
		// 2015.06.29 修改成不退出签约中金额，同时修改订单状态为转让中
		// 退出签约中金额
		/*if (!("0".equals(signFlag) || "1".equals(signFlag))) {
			// 非法参数
			logger.debug("/credit/quickCreditAssign 非法参数--签约中金额退出标志");
			String result = CommonUtil.setResultStringCn(
				new HashMap<String,Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}*/
		
		String signFlag = "0";//签约中金额退出标志：0,不退出签约中金额;1,退出签约中金额
		
		try{
			Map<String, Object> mapParam = new HashMap<String, Object>();
			mapParam.put("userId", userId);
			mapParam.put("orderId", orderId);
			mapParam.put("signFlag", signFlag);
			Map<String, Object> jsonMap = creditAssignService.doQuickCreditAssign(mapParam);
			String result = CommonUtil.setResultStringCn(
				jsonMap,Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE, "");
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		}catch(Exception e){
			logger.debug("/credit/quickCreditAssign 异常" + e.getMessage());
			
			String result = CommonUtil.setResultStringCn(
				new HashMap<String,Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
}
